package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import dao.AgenteDao;
import dao.DaoException;
import dominio.Agente;

/**
 * Servlet implementation class AgenteServlet
 */
public class ListarAgenteServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		List<Agente> agentes = null;
		
		AgenteDao agenteDao = new AgenteDao();
		
		try {
			agentes = agenteDao.findAll(Agente.class);
		} catch (DaoException e) {
			e.printStackTrace();
		}
		
		String listaAgentes = "";
		
		for(Agente a : agentes) {
			listaAgentes += a.getNome() + "\n" + a.getSalario() + ";";
		}
		
		response.getWriter().print(listaAgentes);
		
		response.getWriter().flush();
		response.getWriter().close();
		
	}

}
